package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.mapper.UserMapper;
import com.bwie.utils.R;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* @author 61630
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2024-09-09 09:06:46
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private UserMapper userMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public R getLogin(User user) {
        if(user.getType()==0){
            if(Objects.isNull(user.getUserName())){
                return R.error("账号为空");
            }
            if(Objects.isNull(user.getPassword())){
                return R.error("密码为空");
            }
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getUserName,user.getUserName());
            User users = userMapper.selectOne(wrapper);
            if(Objects.isNull(users)){
                return R.error("用户不存在");
            }
            boolean b = DigestUtil.bcryptCheck(user.getPassword(), users.getPassword());
            if(!b){
                return R.error("密码错误");
            }
            String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie").claim("id", users.getId()).claim("role", users.getRole()).compact();
            ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
            opsForValue.set("token",token,2,TimeUnit.MINUTES);
            HashMap<String, Object> map = new HashMap<>();
            map.put("token",token);
            map.put("role",users.getRole());
            return R.success(map);
        }else {
            if(Objects.isNull(user.getUserName())){
                return R.error("账号为空");
            }
            if(Objects.isNull(user.getCode())){
                return R.error("验证码为空");
            }
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getUserName,user.getUserName());
            User users = userMapper.selectOne(wrapper);
            if(Objects.isNull(users)){
                return R.error("账号错误");
            }
            ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
            String code = opsForValue.get("user:" + user.getUserName());
            if(Objects.isNull(code)){
                return R.error("请先获取验证码");
            }
            if(!code.equals(user.getCode())){
                return R.error("验证码错误");
            }
            String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie").claim("id", users.getId()).claim("role", users.getRole()).compact();
            opsForValue.set("token",token,2,TimeUnit.MINUTES);
            HashMap<String, Object> map = new HashMap<>();
            map.put("token",token);
            map.put("role",users.getRole());
            return R.success(map);
        }
    }

    @Override
    public R sendCode(User user) {
        if(Objects.isNull(user.getUserName())){
            return R.error("账号为空");
        }
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        Boolean flag = stringRedisTemplate.hasKey("user:" + user.getUserName());
        if(flag){
            return R.error("验证码未过期");
        }else {
            Boolean flags = stringRedisTemplate.hasKey("valid:" + user.getUserName());
            if (flags){
                return R.error("验证码失效未到1分钟");
            }else {
                String code = RandomUtil.randomNumbers(4);
                opsForValue.set("user:"+user.getUserName(),code,2,TimeUnit.MINUTES);
                opsForValue.set("valid:"+user.getUserName(),"",3,TimeUnit.MINUTES);
                return R.success("发送成功");
            }
        }
    }

    @Override
    public R findUser(User user) {
        if(Objects.isNull(user.getUserName())){
            return R.error("账号为空");
        }
        if(Objects.isNull(user.getPassword())){
            return R.error("密码为空");
        }
        if(Objects.isNull(user.getCode())){
            return R.error("验证码为空");
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,user.getUserName());
        User users = userMapper.selectOne(wrapper);
        if(Objects.isNull(users)){
            return R.error("账号不存在");
        }
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String code = opsForValue.get("user:" + user.getUserName());
        if(Objects.isNull(code)){
            return R.error("请先获取验证码");
        }
        if(!code.equals(user.getCode())){
            return R.error("验证码错误");
        }
        users.setPassword(DigestUtil.bcrypt(user.getPassword()));
        userMapper.updateById(users);
        return R.success("修改成功");
    }
}




